home *** CD-ROM | disk | FTP | other *** search
/ Internet Info 1993 / Internet Info CD-ROM (Walnut Creek) (1993).iso / networking / osi / isode / dosisode / DOSISODE80.ZIP / ISODE8.WRK / UNIX / LIB / FIDDLE.C < prev    next >
Encoding:
C/C++ Source or Header  |  1992-06-09  |  4.8 KB  |  254 lines

  1. #include <stdio.h>
  2. #include <stddef.h>
  3. #include <varargs.h>
  4. #include <sys/types.h>
  5. #include <sys/ioctl.h>
  6. #include <sys/file.h>
  7. #include <dir.h>
  8. #include <dirent.h>
  9. #include <sys/stat.h>
  10. #include "si_header.h"
  11.  
  12. static int debug = 0;
  13. int si_xlate[] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31};
  14.  
  15. DIR *si_opendir(name)
  16. char *name;
  17. {
  18.     struct stat s;
  19.     char buffer[128];
  20.     int i;
  21.  
  22. if (debug) fprintf(stderr,"opendir trapped %s",name);
  23.     strcpy(buffer,name);
  24.     if (buffer[strlen(buffer)-1] == '/') buffer[strlen(buffer)-1] = '\0';
  25.     i = stat(buffer,&s);
  26. if (debug) fprintf(stderr," stat returns %d\n",i);
  27.     if (i == -1) return(NULL);
  28.     return(opendir(buffer));
  29. }
  30.  
  31. int si_read(s, buf, nbytes)
  32. int s;
  33. char *buf;
  34. int nbytes;
  35. {
  36.     int i;
  37.  
  38. if (debug) fprintf(stderr,"si_read called s = %d\n",s);
  39.     if (__sock[si_x(s)].bit.in_use) return(recvfrom(si_x(s), buf, nbytes, 0, 0, 0));
  40.     else {
  41.         i = read(si_x(s), buf, nbytes);
  42. if (debug) fprintf(stderr,"read s=%d si_xlate=%d nbytes = %d i=%d",s,si_x(s),nbytes,i);
  43.         return(i);
  44.     }
  45. }
  46.  
  47. int si_write(s, buf, nbytes)
  48. int s;
  49. char *buf;
  50. int nbytes;
  51. {
  52. if (debug) fprintf(stderr,"si_write called s=%d si_xlate=%d\n",s,si_x(s));
  53.     if (__sock[si_x(s)].bit.in_use) return(sendto(si_x(s), buf, nbytes, 0, 0, 0));
  54.     else return(write(si_x(s), buf, nbytes));
  55. }
  56.  
  57. si_fclose(fp)
  58. FILE *fp;
  59. {
  60.     if (__sock[fileno(fp)].bit.in_use) return(si_close(fileno(fp)));
  61.     else return(fclose(fp));
  62. }
  63.  
  64. FILE *si_fdopen(s, type)
  65. int s;
  66. char *type;
  67. {
  68.     FILE *fp;
  69.     if (__sock[si_x(s)].bit.in_use) return((FILE *)&_iob[si_x(s)]);
  70.     else {
  71. if (debug) fprintf(stderr,"fdopen called s=%d si_xlate=%d\n",s,si_x(s));
  72.         fp = fdopen(si_x(s), type);
  73.         return(fp);
  74.     }
  75. }
  76.  
  77. si_fprintf(va_alist)
  78. va_dcl
  79. {
  80.     va_list ap;
  81.     char *fmt;
  82.     FILE *fp;
  83.     int cnt, s;
  84.     char buf[1024];
  85.  
  86.     va_start(ap);
  87.     fp = va_arg(ap, FILE *);
  88.     fmt = va_arg(ap, char *);
  89.     s = fileno(fp);
  90.  
  91.     if (__sock[s].bit.in_use) {
  92.         vsprintf(buf, fmt, ap);
  93.         cnt = sendto(s, buf, strlen(buf), 0, 0, 0);
  94.     }
  95.     else cnt = vfprintf(fp, fmt, ap);
  96.  
  97.     va_end(ap);
  98.     return(cnt);
  99. }
  100.  
  101. char *si_fgets(buf, n, fp)
  102. char *buf;
  103. int n;
  104. FILE *fp;
  105. {
  106.     int s, i;
  107.     char b[1];
  108.  
  109. /* we need to read a character string from the socket which is */
  110. /* terminated by a nl. Do this the hard way by reading 1 char  */
  111. /* at a time and testing it */
  112.     s = fileno(fp);
  113.     if (__sock[s].bit.in_use) {
  114.         for (i=1; i<n; i++) {
  115.             if (recvfrom(s, b, 1, 0, 0) == 0)  return(0);
  116.             *buf++ = b[0];
  117.             if (b[0] == '\n') break;
  118.         }
  119.         *buf = '\0';
  120.         return(buf);
  121.     }
  122.     else return(fgets(buf, n, fp));
  123. }
  124.  
  125. si_fgetc(fp)
  126. FILE *fp;
  127. {
  128.     char buf[1];
  129.     int s;
  130.  
  131.     s = fileno(fp);
  132.     if (__sock[s].bit.in_use) {
  133.         if (recvfrom(s, buf, 1, 0, 0) == 0) return(EOF);
  134.         return(buf[0]);
  135.     }
  136.     else return(fgetc(fp));
  137. }
  138.  
  139. si_fputc(c, fp)
  140. char c;
  141. FILE *fp;
  142. {
  143.     int s;
  144.  
  145.     s = fileno(fp);
  146.     if (__sock[s].bit.in_use) return(sendto(s, &c, 1, 0, 0, 0));
  147.     else return(fputc(c, fp));
  148. }
  149.  
  150. int si_fputs(buf, fp)
  151. char *buf;
  152. FILE *fp;
  153. {
  154.     int s;
  155.  
  156.     s = fileno(fp);
  157.     if (__sock[s].bit.in_use) return(sendto(s, buf, strlen(buf), 0, 0, 0));
  158.     else return(fputs(buf, fp));
  159. }
  160.  
  161. si_rewind(fp)
  162. FILE *fp;
  163. {
  164.     int s;
  165.  
  166.     s = fileno(fp);
  167.     if (__sock[s].bit.in_use) return;
  168.     rewind(fp);
  169.     return;
  170. }
  171.  
  172. si_fflush(fp)
  173. FILE *fp;
  174. {
  175.     int s;
  176.  
  177.     s = fileno(fp);
  178.     if (__sock[s].bit.in_use) return(0);
  179.     return(fflush(fp));
  180. }
  181.  
  182. int si_fcntl(s,cmd,arg)
  183. int s, arg;
  184. unsigned int cmd;
  185. {
  186.     int flags;
  187.  
  188.     if (__sock[s].bit.in_use) {
  189.         if (cmd == F_SETFL) {
  190.             __sock[s].bit.non_block  = ((arg & FNDELAY) == FNDELAY);
  191.             if (__sock[s].bit.non_block) si_ioctl(s,FIONBIO,NULL);
  192.             __sock[s].bit.sigio  = ((arg & FASYNC) == FASYNC);
  193.             return(0);
  194.         }
  195.         if (cmd == F_GETFL) {
  196.             flags = 0;
  197.             if (__sock[s].bit.non_block == 1) flags |= FNDELAY;
  198.             if (__sock[s].bit.sigio == 1) flags |= FASYNC;
  199.             return(flags);
  200.         }
  201.         printf("Unexpected cmd to fcntl - %d\n",cmd);
  202.         return(-1);
  203.     }
  204.     return(0); /*fcntl(s, cmd, arg));*/
  205. }
  206.  
  207. void *si_malloc(size)
  208. size_t size;
  209. {
  210.     void *p;
  211.     p = (void *)malloc(size);
  212.     if (p == NULL) {printf("malloc failed size=%d\n",size);getchar();}
  213.     return(p);
  214. }
  215.  
  216. void *si_calloc(number, size)
  217. size_t number, size;
  218. {
  219.     void *p;
  220.     p = (void *)calloc(number, size);
  221.     if (p == NULL) {printf("calloc failed number=%d size=%d\n",number,size);getchar();}
  222.     return(p);
  223. }
  224.  
  225. void *si_realloc(ptr, size)
  226. void *ptr;
  227. size_t size;
  228. {
  229.     void *p;
  230.     p = (void *)realloc(ptr, size);
  231.     if (p == NULL) {printf("realloc failed size=%d\n",size);getchar();}
  232.     return(p);
  233. }
  234.  
  235. void si_free(ptr)
  236. void *ptr;
  237. {
  238.     if (ptr) free(ptr);
  239.     ptr = NULL;
  240. }
  241.  
  242. void si_cfree(ptr)
  243. void *ptr;
  244. {
  245.     if (ptr) free(ptr);
  246.     ptr = NULL;
  247. }
  248.  
  249. int si_x(s)
  250. int s;
  251. {
  252.     if (s>=0 && s<32) return(si_xlate[s]);
  253.     else return(-1);
  254. }